<?php

use think\migration\Migrator;
use think\migration\db\Column;

class InitTables extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        // 1. 创建ticket表（水票表）
        $ticket = $this->table('ticket', ['id' => false, 'primary_key' => ['id']]);
        $ticket->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
               ->addColumn('image', 'string', ['limit' => 255, 'comment' => '封面图'])
               ->addColumn('title', 'string', ['limit' => 255, 'comment' => '标题'])
               ->addColumn('remark', 'text', ['null' => true, 'comment' => '备注'])
               ->addColumn('money', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '金额'])
               ->addColumn('status', 'integer', ['default' => 1, 'comment' => '状态'])
               ->addColumn('free', 'integer', ['default' => 0, 'comment' => '免费数量'])
               ->addColumn('buy', 'integer', ['default' => 0, 'comment' => '多少桶'])
               ->addColumn('recommended', 'integer', ['default' => 0, 'comment' => '是否推荐，0不推荐，1推荐'])
               ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
               ->addColumn('update_time', 'timestamp', ['null' => true, 'comment' => '更新时间'])
               ->create();

        // 2. 创建type表（类型表）
        $type = $this->table('type', ['id' => false, 'primary_key' => ['id']]);
        $type->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
             ->addColumn('name', 'string', ['limit' => 255, 'comment' => '类型名'])
             ->addColumn('status', 'integer', ['default' => 1, 'comment' => '状态'])
             ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
             ->addColumn('update_time', 'timestamp', ['null' => true, 'comment' => '更新时间'])
             ->create();

        // 3. 创建specification表（规格表）
        $specification = $this->table('specification', ['id' => false, 'primary_key' => ['id']]);
        $specification->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
                      ->addColumn('type_id', 'integer', ['signed' => false, 'comment' => '类型ID'])
                      ->addColumn('specification', 'string', ['limit' => 255, 'comment' => '具体规格'])
                      ->addColumn('status', 'integer', ['default' => 1, 'comment' => '状态'])
                      ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
                      ->addColumn('update_time', 'timestamp', ['null' => true, 'comment' => '更新时间'])
                      ->addIndex(['type_id'])
                      ->create();

        // 4. 创建detail表（产品详情表）
        $detail = $this->table('detail', ['id' => false, 'primary_key' => ['id']]);
        $detail->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
               ->addColumn('type_id', 'integer', ['signed' => false, 'comment' => '类型ID'])
               ->addColumn('specification', 'string', ['limit' => 255, 'comment' => '具体规格'])
               ->addColumn('images', 'text', ['null' => true, 'comment' => '产品图片'])
               ->addColumn('money', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '金额'])
               ->addColumn('transport', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '运费'])
               ->addColumn('status', 'integer', ['default' => 1, 'comment' => '状态'])
               ->addColumn('recommended', 'integer', ['default' => 0, 'comment' => '是否推荐，0不推荐，1推荐'])
               ->addColumn('num', 'integer', ['default' => 0, 'comment' => '产品数量'])
               ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
               ->addColumn('update_time', 'timestamp', ['null' => true, 'comment' => '更新时间'])
               ->addIndex(['type_id'])
               ->create();

        // 5. 创建order表（订单表）
        $order = $this->table('order', ['id' => false, 'primary_key' => ['id']]);
        $order->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
              ->addColumn('order_id', 'string', ['limit' => 50, 'comment' => '订单号'])
              ->addColumn('money', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '订单金额'])
              ->addColumn('order_detail', 'text', ['null' => true, 'comment' => '订单详情'])
              ->addColumn('status', 'integer', ['default' => 0, 'comment' => '支付状态'])
              ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '下单时间'])
              ->addColumn('pay_time', 'timestamp', ['null' => true, 'comment' => '支付时间'])
              ->addColumn('pay_money', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '支付金额'])
              ->addColumn('address_id', 'integer', ['signed' => false, 'null' => true, 'comment' => '地址ID'])
              ->addColumn('ticket_num', 'integer', ['default' => 0, 'comment' => '使用水票数量'])
              ->addColumn('bucket', 'integer', ['default' => 0, 'comment' => '是否有水桶，0没有，1有'])
              ->addColumn('delivery_money', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '配送费用'])
              ->addColumn('remark', 'text', ['null' => true, 'comment' => '备注'])
              ->addColumn('deliver_water_user_id', 'integer', ['signed' => false, 'null' => true, 'comment' => '送水工ID'])
              ->addColumn('code', 'string', ['limit' => 50, 'null' => true, 'comment' => '核销码'])
              ->addIndex(['order_id'])
              ->addIndex(['address_id'])
              ->addIndex(['deliver_water_user_id'])
              ->create();

        // 6. 创建cart表（购物车表）
        $cart = $this->table('cart', ['id' => false, 'primary_key' => ['id']]);
        $cart->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
             ->addColumn('detail_id', 'integer', ['signed' => false, 'comment' => '产品详情ID'])
             ->addColumn('user_id', 'integer', ['signed' => false, 'comment' => '用户ID'])
             ->addColumn('num', 'integer', ['default' => 1, 'comment' => '数量'])
             ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
             ->addIndex(['detail_id'])
             ->addIndex(['user_id'])
             ->create();

        // 7. 创建address表（地址表）
        $address = $this->table('address', ['id' => false, 'primary_key' => ['id']]);
        $address->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
                ->addColumn('name', 'string', ['limit' => 100, 'comment' => '名字'])
                ->addColumn('phone', 'string', ['limit' => 20, 'comment' => '手机号码'])
                ->addColumn('address', 'text', ['comment' => '送货地址'])
                ->addColumn('status', 'integer', ['default' => 0, 'comment' => '1：默认，0：不是默认'])
                ->addColumn('user_id', 'integer', ['signed' => false, 'comment' => '用户ID'])
                ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
                ->addColumn('update_time', 'timestamp', ['null' => true, 'comment' => '更新时间'])
                ->addIndex(['user_id'])
                ->create();

        // 8. 创建promotion表（推广表）
        $promotion = $this->table('promotion', ['id' => false, 'primary_key' => ['id']]);
        $promotion->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
                  ->addColumn('user_id', 'integer', ['signed' => false, 'comment' => '用户ID'])
                  ->addColumn('pid', 'integer', ['signed' => false, 'null' => true, 'comment' => '上级'])
                  ->addColumn('type', 'integer', ['default' => 0, 'comment' => '0:积分，1:水滴'])
                  ->addColumn('point', 'integer', ['default' => 0, 'comment' => '积分或者水滴'])
                  ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
                  ->addIndex(['user_id'])
                  ->addIndex(['pid'])
                  ->create();

        // 9. 创建userticket表（用户水票表）
        $userticket = $this->table('userticket', ['id' => false, 'primary_key' => ['id']]);
        $userticket->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
                   ->addColumn('ticket_id', 'integer', ['signed' => false, 'comment' => '水票ID'])
                   ->addColumn('user_id', 'integer', ['signed' => false, 'comment' => '用户ID'])
                   ->addColumn('type_ids', 'text', ['null' => true, 'comment' => '可以购买的类型'])
                   ->addColumn('source', 'integer', ['default' => 1, 'comment' => '来源，1是自购，2是抽奖'])
                   ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
                   ->addIndex(['ticket_id'])
                   ->addIndex(['user_id'])
                   ->create();

        // 10. 创建order_ticket表（订单水票表）
        $orderTicket = $this->table('order_ticket', ['id' => false, 'primary_key' => ['id']]);
        $orderTicket->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
                    ->addColumn('user_id', 'integer', ['signed' => false, 'comment' => '用户ID'])
                    ->addColumn('money', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '购买金额'])
                    ->addColumn('pay_money', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '支付金额'])
                    ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
                    ->addColumn('pay_time', 'timestamp', ['null' => true, 'comment' => '支付时间'])
                    ->addColumn('status', 'integer', ['default' => 0, 'comment' => '状态，1是已支付'])
                    ->addIndex(['user_id'])
                    ->create();
    }
}
